package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.IndexConstant;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.ContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private ContentService contentService;

    @RequestMapping({"/","toIndexShow"})
    private String toIndexShow(){
        return "index";
    }

    @RequestMapping("/listMyCarousel")
    @ResponseBody
    public Result<List<TbContent>> listMyCarousel(){
        try {
            List<TbContent> tbContentList = (List<TbContent>) redisTemplate.opsForHash().get(IndexConstant.CAROUSEL_KEY,IndexConstant.CAROUSEL_FIELD);
            Optional<List<TbContent>> op = Optional.ofNullable(tbContentList);
            tbContentList = op.orElseGet(() ->{
                List<TbContent> tbContents = contentService.getListMyCarousel();
                redisTemplate.opsForHash().put(IndexConstant.CAROUSEL_KEY,IndexConstant.CAROUSEL_FIELD,tbContents);
                redisTemplate.expire(IndexConstant.CAROUSEL_KEY,2, TimeUnit.HOURS);
                return tbContents;
            });
            return Result.jsonResponse(ResultEnum.OK).setData(tbContentList);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("/listTodayContent")
    @ResponseBody
    public Result<List<TbContent>> listTodayContent(){
        try {
            List<TbContent> tbContentList = (List<TbContent>) redisTemplate.opsForHash().get(IndexConstant.TODAY_KEY,IndexConstant.TODAY_FIELD);
            Optional<List<TbContent>> op = Optional.ofNullable(tbContentList);
            tbContentList = op.orElseGet(() ->{
                List<TbContent> tbContents = contentService.listTodayContent();
                redisTemplate.opsForHash().put(IndexConstant.TODAY_KEY,IndexConstant.TODAY_FIELD,tbContents);
                redisTemplate.expire(IndexConstant.TODAY_KEY,2, TimeUnit.HOURS);
                return tbContents;
            });
            return Result.jsonResponse(ResultEnum.OK).setData(tbContentList);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("/listFavoriteContent")
    @ResponseBody
    public Result<List<TbContent>> listFavoriteContent(){
        try {
            List<TbContent> tbContentList = (List<TbContent>) redisTemplate.opsForHash().get(IndexConstant.FAVORITE_KEY,IndexConstant.FAVORITE_FIELD);
            Optional<List<TbContent>> op = Optional.ofNullable(tbContentList);
            tbContentList = op.orElseGet(() ->{
                List<TbContent> tbContents = contentService.listFavoriteContent();
                redisTemplate.opsForHash().put(IndexConstant.FAVORITE_KEY,IndexConstant.FAVORITE_FIELD,tbContents);
                redisTemplate.expire(IndexConstant.TODAY_KEY,2, TimeUnit.HOURS);
                return tbContents;
            });
            return Result.jsonResponse(ResultEnum.OK).setData(tbContentList);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("/listInterestContent")
    @ResponseBody
    public Result<List<TbContent>> listInterestContent(){
        try {
            List<TbContent> tbContentList = (List<TbContent>) redisTemplate.opsForHash().get(IndexConstant.INTERESTING_KEY,IndexConstant.INTERESTING_FIELD);
            Optional<List<TbContent>> op = Optional.ofNullable(tbContentList);
            tbContentList = op.orElseGet(() ->{
                List<TbContent> tbContents = contentService.listInterestContent();
                redisTemplate.opsForHash().put(IndexConstant.INTERESTING_KEY,IndexConstant.INTERESTING_FIELD,tbContents);
                redisTemplate.expire(IndexConstant.INTERESTING_KEY,2, TimeUnit.HOURS);
                return tbContents;
            });
            return Result.jsonResponse(ResultEnum.OK).setData(tbContentList);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    //今日推荐
    @RequestMapping("/listTab1Content")
    @ResponseBody
    public Result<List<TbContent>> listTab1Content(){
        try {
            List<TbContent> tbContentList = (List<TbContent>) redisTemplate.opsForHash().get(IndexConstant.TAB1_KEY,IndexConstant.TAB1_FIELD);
            Optional<List<TbContent>> op = Optional.ofNullable(tbContentList);
            tbContentList = op.orElseGet(() ->{
                List<TbContent> tbContents = contentService.listTab1Content();
                redisTemplate.opsForHash().put(IndexConstant.TAB1_KEY,IndexConstant.TAB1_FIELD,tbContents);
                redisTemplate.expire(IndexConstant.TAB1_KEY,2, TimeUnit.HOURS);
                return tbContents;
            });
            return Result.jsonResponse(ResultEnum.OK).setData(tbContentList);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }
}
